<?php
/**
 * Created by PhpStorm.
 * User: clkj1
 * Date: 2018/7/13
 * Time: 13:53
 */

session_start();
require_once './core/sql.php';
require_once './makePoster.class.php';
$sql = new SqlS();
$makePoster = new makePoster();

if (isset($_POST['type'])) {
    $type = $_POST['type'];
    switch ($type) {
        case 'goMakeForm':
//            $_SESSION['listArray'] = [[], []];
            exit('{"content":"0"}');
            break;
        case 'addCoursesInTemp':
            if (isset($_POST['listJson'])) {
                $_SESSION['listArray'] = json_decode($_POST['listJson'], true);
                $_SESSION['title'] = $_POST['title'];
                exit('{"content":"0"}');
            } else {
                exit('{"content":"1","error":"no.1"}');
            }
            break;
        case 'backMakeFrom':
            $sql->addCourses($_POST['listJson'], $_POST['id'], $_SESSION['f-uid']);
            exit('{"content":"0"}');
            break;
        case 'inputDiyQuestion':
            if ($sql->inputDiyQuestion($_POST['quId'], $_POST['quName'], $_POST['isShow'], $_POST['quType'], $_POST['listJson'], $_SESSION['f-uid']))
                exit('{"content":"0"}');
            else
                exit('{"content":"1"}');
            break;
        case 'makeQrCodeNow':
            $sql->makeQrCodeNow($_SESSION['f-uid'], $_SESSION['listArray'], $_SESSION['f-fid'], $_SESSION['title']);
            $host = $_SERVER['HTTP_HOST'];
            $uid = $_SESSION['f-uid'];
            $fid = $_SESSION['f-fid'];
            $_SESSION['code'] = $makePoster->makeQRCode($_SESSION['f-uid'], "http://$host/form.php?uid=$uid&fid=$fid");
            exit('{"content":"0"}');
            break;
        case 'outPoster':
            $_SESSION['image-bg'] = $_POST['bg'];
            $_SESSION['image-title'] = $_POST['title'];
            $_SESSION['tp'] = $_POST['tp'];
            $su = require_once './poster.php';
            if ($su == 'success')
                exit(json_encode(['content' => 0]));
            else
                exit(json_encode(['content' => 1]));
            break;
        case 'addSession':
            if (isset($_POST['id'])) {
                switch ($_POST['id']) {
                    case '1':
                        // 背景：
                        $_SESSION['image-bg-id'] = $sql->changeDeImageTextToId($_POST['bg'], 1);
                        exit('{"content":"0"}');
                        break;
                    case '2':
                        // 标题
                        $_SESSION['image-title-id'] = $sql->changeDeImageTextToId($_POST['title'], 2);
                        exit('{"content":"0"}');
                        break;
                    case '3':
                        // 图片版式
                        $_SESSION['image-format-id'] = $sql->changeDeImageTextToId($_POST['format'], 3);
                        exit('{"content":"0"}');
                        break;
                    default:
                        exit('{"content":"1"}');
                }
            } else {
                exit('{"content":"1"}');
            }
            break;
        case 'addText':
            switch ($_POST['num']) {
                case '#text1':
                    $_SESSION['text1'] = $_POST['text'];
                    exit('{"content":"0"}');
                    break;
                case '#text2':
                    $_SESSION['text2'] = $_POST['text'];
                    exit('{"content":"0"}');
                    break;
                case '#text3':
                    $_SESSION['text3'] = $_POST['text'];
                    exit('{"content":"0"}');
                    break;
                default:
                    exit('{"content":"1"}');
            }
            break;
        case 'cutCode':
            $_SESSION['code'] = $makePoster->cutCode($_POST['image_width'], $_POST['image_height'], $_POST['cut_xy'], $_POST['cut_width'], $_POST['cut_height'], $_SESSION['image-code']);
            exit('{"content":"0"}');
            break;
        case 'uploadForm':
            $form_array = json_decode($_POST['form'], true);
            $result_array = $sql->addForm($form_array, $_POST['cid'], $_POST['fid']);
            exit('{"content":"' . $result_array[0] . '","message":' . $result_array[1] . ',"name":' . $result_array[2] . '}');
            break;
        case 'addImInTa':
            switch ($_POST['sType']) {
                case 'save':
                    $res = $sql->addImInTa($_SESSION['f-uid'], $_SESSION['f-fid'], $_POST['url']);
                    if ($res == 0) {
                        $_SESSION['save'] = 1;
                    }
                    exit(json_encode(['content' => $res]));
                    break;
                case 'share':
                    if ($sql->addImInTa($_SESSION['f-uid'], $_SESSION['f-fid'], $_POST['url']) === 0) {
                        exit(json_encode(['content' => 0]));
                    }
                    break;
                default:
            }
            break;
        case 'editPoster':
            if (isset($_POST['uid']) && isset($_POST['pid'])) {
                $_SESSION['f-uid'] = $_POST['uid'];
                $_SESSION['f-fid'] = $_POST['pid'];
                $host = $_SERVER['HTTP_HOST'];
                $uid = $_SESSION['f-uid'];
                $fid = $_SESSION['f-fid'];
                $_SESSION['code'] = $makePoster->makeQRCode($_SESSION['f-uid'], "http://$host/form.php?uid=$uid&fid=$fid");
                exit('{"state":0}');
            } else {
                exit('{"state":1,"error":"?"}');
            }
            break;
        case 'deleteDiyQuestion':
            if (isset($_POST['quId'])) {
                $res = $sql->deleteDitQuestion($_SESSION['uid'], $_POST['quId']);
                if ($res == 0)
                    exit(json_encode(['content' => 0]));
                else
                    exit(json_encode(['content' => 1]));
            } else {
                exit(json_encode(['content' => 1]));
            }
            break;
        default :
    }
}